package com.ct.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

import com.ct.entity.enums.PageSize;
import com.ct.entity.po.Travel;
import com.ct.entity.query.SimplePage;
import com.ct.entity.query.TravelQuery;
import com.ct.entity.vo.PaginationResultVO;
import com.ct.mappers.TravelMapper;
import com.ct.service.TravelService;
import com.ct.service.client.ApprovalClient;
import com.ct.utils.StringTools;

import jakarta.annotation.Resource;

/**
 * 出差表 业务接口实现
 */
@Service("travelService")
public class TravelServiceImpl implements TravelService {

    @Resource
    private TravelMapper<Travel, TravelQuery> travelMapper;

    // 这个就是你想使用其他service的具体声明
    @Resource
    private ApprovalClient approvalClient;

    /**
     * 根据条件查询列表
     */
    @Override
    public List<Travel> findListByParam(TravelQuery param) {
        return this.travelMapper.selectList(param);
    }

    /**
     * 根据条件查询列表
     */
    @Override
    public Integer findCountByParam(TravelQuery param) {
        return this.travelMapper.selectCount(param);
    }

    /**
     * 分页查询方法
     */
    @Override
    public PaginationResultVO<Travel> findListByPage(TravelQuery param) {
        int count = this.findCountByParam(param);
        int pageSize = param.getPageSize() == null ? PageSize.SIZE15.getSize() : param.getPageSize();

        SimplePage page = new SimplePage(param.getPageNo(), count, pageSize);
        param.setSimplePage(page);
        List<Travel> list = this.findListByParam(param);
        PaginationResultVO<Travel> result = new PaginationResultVO(count, page.getPageSize(), page.getPageNo(),
                page.getPageTotal(), list);
        return result;
    }

    /**
     * 新增
     */
    @Override
    public Integer add(Travel bean) {
        return this.travelMapper.insert(bean);
    }

    /**
     * 批量新增
     */
    @Override
    public Integer addBatch(List<Travel> listBean) {
        if (listBean == null || listBean.isEmpty()) {
            return 0;
        }
        return this.travelMapper.insertBatch(listBean);
    }

    /**
     * 新增出差申请
     */
    @Override
    public Integer addTravel(Travel travel) {
        return this.travelMapper.insertTravel(travel);
    }

    /**
     * 批量新增或者修改
     */
    @Override
    public Integer addOrUpdateBatch(List<Travel> listBean) {
        if (listBean == null || listBean.isEmpty()) {
            return 0;
        }
        return this.travelMapper.insertOrUpdateBatch(listBean);
    }

    /**
     * 多条件更新
     */
    @Override
    public Integer updateByParam(Travel bean, TravelQuery param) {
        StringTools.checkParam(param);
        return this.travelMapper.updateByParam(bean, param);
    }

    /**
     * 多条件删除
     */
    @Override
    public Integer deleteByParam(TravelQuery param) {
        StringTools.checkParam(param);
        return this.travelMapper.deleteByParam(param);
    }

    /**
     * 根据TravelId获取对象
     */
    @Override
    public Travel getTravelByTravelId(Integer travelId) {
        return this.travelMapper.selectByTravelId(travelId);
    }

    /**
     * 根据TravelId修改
     */
    @Override
    public Integer updateTravelByTravelId(Travel bean, Integer travelId) {
        return this.travelMapper.updateByTravelId(bean, travelId);
    }

    /**
     * 根据TravelId删除
     */
    @Override
    public Integer deleteTravelByTravelId(Integer travelId) {
        return this.travelMapper.deleteByTravelId(travelId);
    }

    @Override
    public List<Travel> getAllTravelsByIdList(List<String> travelIdList) {
        ArrayList<Travel> travelList = new ArrayList<>();
        for (String travelId : travelIdList) {
            Travel travel = this.getTravelByTravelId(Integer.parseInt(travelId));
            if (null != travel) {
                travelList.add(travel);
            }
        }
        return travelList;
    }

    /**
     * 根据userId查询TravelList(前端需要查看)(筛选后的)
     * 
     * @param userId
     * @return
     */
    @Override
    public List<Travel> findFinalListByUserId(String userId) {
        List<Travel> travelList = this.travelMapper.selectFinalByUserId(userId);
        return travelList;
    }

    @Override
    public List<Travel> getAllTravelListByUserId(String userId) {
        TravelQuery travelQuery = new TravelQuery();
        travelQuery.setUserId(userId);
        return this.travelMapper.selectList(travelQuery);
    }
}